package com.cloudcross.ssp.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cloudcross.ssp.common.utils.DoublePreciseCompute;
import com.cloudcross.ssp.model.rpt.OrderRpt;
import com.cloudcross.ssp.model.rpt.Report;

public class ObjectBase {
    private Logger logger = LogManager.getLogger(getClass());
    
    public JSONArray getJSONArray(JSONObject obj, String key) {
        JSONArray jsonArray = new JSONArray();
        try {
            if (obj.get(key) != null) jsonArray = obj.getJSONArray(key);
        } catch (JSONException e) {
            logger.error("json exception:" + e.getMessage());
        }
        return jsonArray;
    }

    public String getJSONObjectString(JSONObject obj, String key, String defaultValue) {
        String ret = defaultValue;
        try {
            ret = obj.getString(key);
            if (ret == null || ret.isEmpty())   ret = defaultValue;
        } catch (JSONException e) {
            logger.error("json exception:" + e.getMessage());
        }
        return ret;
    }

    public String getString(Object obj) {
        String ret = "";
        try {
            ret = String.valueOf(obj);
            if (isNULLStr(ret))
                ret = "";
        } catch (Exception e) {
        }
        return ret;
    }

    public static boolean isNULLStr(String input) {
        if (input == null) {
            return true;
        }
        if (StringUtils.isBlank(input)) {
            return true;
        }
        if (input.trim().equals("")) {
            return true;
        }
        return input.equalsIgnoreCase("null");
    }

    public static boolean isNotNull(String str) {
        return str != null && !str.equals("");
    }

    public long getLong(Object obj) {
        long ret = 0;
        try {
            ret = Long.parseLong(String.valueOf(obj));
        } catch (Exception e) {
            logger.error("parse exception:" + e.getMessage());
        }
        return ret;
    }

    public double getDouble(Object obj) {
        double ret = 0;
        try {
            ret = Double.parseDouble(String.valueOf(obj));
        } catch (Exception e) {
            logger.error("parse exception:" + e.getMessage());
        }
        return ret;
    }

    public float getFloat(Object obj) {
        float ret = 0;
        try {
            ret = Float.parseFloat(String.valueOf(obj));
        } catch (Exception e) {
            logger.error("parse exception:" + e.getMessage());
        }
        return ret;
    }

    public String getJSONObjectBoolean(JSONObject obj, String key) {
        String ret = "0";
        try {
            ret = obj.getString(key);
            if (ret.isEmpty()) {
                ret = "0";
            } else if ("true".equals(ret)) {
                ret = "1";
            }
        } catch (JSONException e) {
            logger.error("json exception:" + e.getMessage());
        }
        return ret;

    }

    public String getJSONObjectLong(JSONObject obj, String key) {
        String ret = "0";
        try {
            ret = obj.getString(key);
            if (ret.isEmpty())
                ret = "0";
        } catch (JSONException e) {
            logger.error("json exception:" + e.getMessage());
        }
        return ret;
    }

    public double getJSONObjectDouble(JSONObject obj, String key, double defaultValue) {
        double ret = 0;
        try {
            java.text.DecimalFormat myformat = new java.text.DecimalFormat("#0.00");
            ret = obj.getDouble(key);// 装换为double类型
            ret = Double.parseDouble(myformat.format(ret));// 保留2为小数
        } catch (Exception e) {
            logger.error("json exception:" + e.getMessage());
        }
        return ret;
    }

    public float getJSONObjectFloat(JSONObject obj, String key, float defaultValue) {
        float ret = 0;
        try {
            ret = Float.parseFloat(obj.getString(key));
            java.text.DecimalFormat myformat = new java.text.DecimalFormat("#0.00");
            ret = Float.parseFloat(myformat.format(ret));// 装换为double类型
        } catch (Exception e) {
            logger.error("parse exception:" + e.getMessage());
        }
        return ret;
    }

    public int getJSONObjectInt(JSONObject obj, String key, int defaultValue) {
        int ret = 0;
        try {
            ret = obj.getInt(key);
        } catch (Exception e) {
            logger.error("parse exception:" + e.getMessage());
        }
        return ret;
    }

    public long getJSONObjectLong(JSONObject obj, String key, int defaultValue) {
        long ret = defaultValue;
        try {
            ret = obj.getLong(key);
        } catch (Exception e) {
            logger.error("json exception:" + e.getMessage());
            ret = defaultValue;
        }
        return ret;
    }

    public JSONObject getJSONObject(JSONObject obj, String key) {
        JSONObject ret = null;
        try {
            ret = obj.getJSONObject(key);
            if (ret.isEmpty()) {
                ret = null;
            }
        } catch (JSONException e) {
            logger.error("json exception:" + e.getMessage());
        }
        return ret;
    }
    
    public static List<OrderRpt> compareRptOrderValue(Report orderRpt,Report orderHourRpt,List<OrderRpt> reportList){
        int shows      = orderRpt.getShows() -orderHourRpt.getShows();
        int clicks     = orderRpt.getClicks() - orderHourRpt.getClicks();
        float consumes = orderRpt.getConsumes()- orderHourRpt.getConsumes();
        if (orderRpt.equals(orderHourRpt) == false){
            for (int i = 0; i < reportList.size(); i++) {
                OrderRpt hourRpt = reportList.get(i);
                if(hourRpt.getHour() == 2){
                    hourRpt.setShow(hourRpt.getShow() + shows);
                    hourRpt.setClick(hourRpt.getClick() + clicks);
                    hourRpt.setConsume(hourRpt.getConsume() + consumes);
                    hourRpt.setEcpm(DoublePreciseCompute.div(hourRpt.getConsume()*1000d,hourRpt.getShow()));
                    hourRpt.setClickRate(DoublePreciseCompute.div(hourRpt.getClick()*100d,hourRpt.getShow()));
                    reportList.set(i, hourRpt);
                }
            }
        }
        return reportList;
    }
}
